home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
modprolg
/
mod-prol.lha
/
Prolog
/
Examples
/
chart.mod
next >
Wrap
Text File
|
1992-05-30
|
5KB
|
168 lines
% Reference :
% G.Gazdar and C.S.Mellish, "Natural Language Processing in Prolog :
% An Introduction to Computational Linguistics", 1989, Addison-Wesley
signature agenda_sig =
sig
pred add_to_agenda/4 and remove_from_agenda/4 and
erase_agenda/0 and agenda_data/4.
end.
structure agenda/agenda_sig =
struct
pred agenda_data/4.
add_to_agenda(V0,V1,C,Needed) :-
agenda_data(V0,V1,C,Needed) ->
true ;
asserta(agenda_data(V0,V1,C,Needed)).
remove_from_agenda(V0,V1,C,Needed) :-
retract(agenda_data(V0,V1,C,Needed)).
erase_agenda :-
retractall(agenda_data(_,_,_,_)).
end.
signature chart_sig =
sig
pred add_chart_edge/4 and erase_chart/0 and chart_edge/4.
end.
structure chart/chart_sig =
struct
pred chart_edge/4.
add_chart_edge(V0,V1,C,Needed) :-
chart_edge(V0,V1,C,Needed) ->
true ;
asserta(chart_edge(V0,V1,C,Needed)).
erase_chart :-
retractall(chart_edge(_,_,_,_)).
end.
signature grammar_sig =
sig
pred initial/1 and rule/2 and lex/2 and tag/1.
end.
structure grammar1/grammar_sig =
struct
tag(X) :-
current_structure(X).
initial(s).
rule(s,[np,vp]).
rule(np,[det,nb]).
rule(nb,[n]).
rule(nb,[n,rel]).
rule(rel,[wh,vp]).
rule(vp,[iv]).
rule(vp,[tv,np]).
rule(vp,[dv,np,pp]).
rule(vp,[sv,s]).
rule(pp,[p,np]).
lex(np,[kim]).
lex(np,[sandy]).
lex(np,[lee]).
lex(np,[bread]).
lex(det,[a]).
lex(det,[the]).
lex(det,[her]).
lex(n,[consumer]).
lex(n,[duck]).
lex(n,[man]).
lex(n,[woman]).
lex(wh,[who]).
lex(wh,[that]).
lex(p,[to]).
lex(iv,[died]).
lex(iv,[ate]).
lex(tv,[ate]).
lex(tv,[saw]).
lex(tv,[gave]).
lex(dv,[gave]).
lex(dv,[handed]).
lex(sv,[knew]).
end.
structure utils =
struct
% For each X do Y.
foreach(X,Y) :-
call(X),
do(Y),
fail.
foreach(_,_).
do(Y) :-
call(Y),!.
% Read in a sentence, terminated by a '.' and tag
% each word with Tag.
get_sentence(Wordlist,Tag) :-
get0(Char),
getrest(Char,Wordlist,Tag).
getrest(46,[],_) :- !.
getrest(32,Wordlist,Tag) :- !,
get_sentence(Wordlist,Tag).
getrest(Letter,[Word|Wordlist],Tag) :-
getletters(Letter,Letters,Nextchar),
name(Word,Letters,Tag),
getrest(Nextchar,Wordlist,Tag).
getletters(46,[],46) :- !.
getletters(32,[],32) :- !.
getletters(Let,[Let|Letters],Nextchar) :-
get0(Char),
getletters(Char,Letters,Nextchar).
end.
functor abs_parser(x/grammar_sig,y/agenda_sig,z/chart_sig) =
struct
inherit utils.
structure grammar = x.
structure agenda = y.
structure chart = z.
start :-
agenda:erase_agenda,
chart:erase_chart,
grammar:tag(Tag),
utils:get_sentence(Sentence,Tag),
grammar:initial(Symbol),
start_agenda(Sentence,0),
start_active(Symbol,0),
expand_edges,
chart:chart_edge(0,_,Symbol,[]).
start_agenda([],_).
start_agenda([Word|Words],V0) :-
V1 is V0 + 1,
utils:foreach(grammar:lex(Category,[Word]),
agenda:add_to_agenda(V0,V1,Category,[])),
start_agenda(Words,V1).
start_active(Category,Vertex) :-
utils:foreach(grammar:rule(Category,Categories),
agenda:add_to_agenda(Vertex,Vertex,Category,Categories)).
expand_edges :-
agenda:remove_from_agenda(V0,V1,Category,Needed) ,!,
extend_edges(V0,V1,Category,Needed).
expand_edges.
extend_edges(V0,V1,Category,Needed) :-
chart:chart_edge(V0,V1,Category,Needed),
expand_edges.
extend_edges(V0,V1,Category,Needed) :-
chart:add_chart_edge(V0,V1,Category,Needed),
new_edges(V0,V1,Category,Needed),
expand_edges.
new_edges(V1,V2,Category1,[]) :-
% Inactive edges
utils:foreach(chart:chart_edge(V0,V1,Category2,
[Category|Categories]),
agenda:add_to_agenda(V0,V2,Category2,Categories)).
new_edges(V1,V2,Category1,[Category2|Categories]) :-
% Active edges
start_active(Category2,V2),
utils:foreach(chart:chart_edge(V2,V3,Category2,[]),
agenda:add_to_agenda(V1,V3,Category1,Categories)).
end.
structure final = abs_parser(grammar1,agenda,chart).